﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：PurchaseCompanyUpdate.cs
    // 文件功能描述：设置采购目录的配送企业
    // 创建标识：吕海乐2009-05-15
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using System.Collections;
using System.Data;

namespace HZ.DrugTrade.Web.Hospitals.Procurement
{
    public partial class PurchaseCompanyUpdate : BasePage
    {
        public PurchaseCompanyUpdate()
        {
            this.ModuleNo = "";
        }

        IBLL_Distribution id = Factory.BLL_Distribution();
        IBLL_HospitalProcureCatalog hpcCtrl = Factory.BLL_HospitalProcureCatalog();
        IBLL_ProcurementBasic pbCtrl = Factory.BLL_ProcurementBasic();
        public bool BoolSelect=false;
        protected void Page_Load(object sender, EventArgs e)
        {
            string procureCatalogId = Request.QueryString["procureCatalogId"];
            //by ligang 2009-6-15
            HZ.DrugTrade.Model.View.ProcureCatalog procureCatalog = pbCtrl.GetProcureCatalogByID(procureCatalogId);
            ViewState["Projectid"] = string.IsNullOrEmpty(procureCatalog.ProjectID)?string.Empty:procureCatalog.ProjectID;
            if (procureCatalog != null)
            {
                //非采购产品单独设置配送企业 0 竞价议价，1重点监控，2，自行采购，3临时采购
                if ((procureCatalog.SortSource??3) >= 3)
                {
                    Server.Transfer("PurchaseCompanyUpdate_UnBid.aspx", true);
                }
            }
            else
            {
                PageError("采购产品不存在！", this.ReturnUrl);
            }

            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["CompanyID_PS"]))
                {
                    hfdHospitalInfo.Value = Request.QueryString["CompanyID_PS"];
                }
                if (!string.IsNullOrEmpty(Request.QueryString["State"]))
                {
                    gvwCompany.Columns[3].Visible = false;
                }
                BindAllCompanyList();
            }
        }

        /// <summary>
        /// 绑定所有可选的配送企业
        /// </summary>
        private void BindAllCompanyList()
        {
            string procureCatalogId = Request.QueryString["procureCatalogId"];
            string hospitalId = CurrentAccount.GetUser().Org.OrgID;
            //by ligang 2009-6-15
            int tmpCount;
            HZ.DrugTrade.Model.View.HospitalProcureCatalog hospitalProcureCatalog = null;
            IList<HZ.DrugTrade.Model.View.HospitalProcureCatalog> hospitalProcureCatalogList = hpcCtrl.GetHospitalProcureCatalog(
                new HZ.DrugTrade.Model.Query.HospitalProcureCatalog() { ProcureCatalogID = procureCatalogId, HospitalID = hospitalId }, 1, 0, out tmpCount);
            if (hospitalProcureCatalogList.Count != 0)
            {
                hospitalProcureCatalog = hpcCtrl.GetHospitalProcureCatalog(
                       new HZ.DrugTrade.Model.Query.HospitalProcureCatalog() { ProcureCatalogID = procureCatalogId, HospitalID = hospitalId }, 1, 0, out tmpCount)[0];

                if (hospitalProcureCatalog != null)
                {
                    this.lblGoodsName.Text = hospitalProcureCatalog.GoodsName;
                    this.lblMaterial.Text = hospitalProcureCatalog.Material;
                    this.lblGg.Text = hospitalProcureCatalog.Outlookc;
                    this.lblCompanyName_SC.Text = hospitalProcureCatalog.CompanyName_SC;
                    this.lblCompanyName_ZB.Text = hospitalProcureCatalog.CompanyName_TB;
                    this.lblBidPrice.Text = (hospitalProcureCatalog.ProcurePrice.Value == 0) ? "NA" : ViewState["Projectid"].ToString() == "0002" ? hospitalProcureCatalog.ProcurePrice.Value.ToString("0.00") : hospitalProcureCatalog.ProcurePrice.Value.ToString("0.0000");
                    this.lblPrice.Text = (hospitalProcureCatalog.Price == null) ? "NA" : hospitalProcureCatalog.Price.Value == 0 ? "NA" : ViewState["Projectid"].ToString() == "0002" ? hospitalProcureCatalog.Price.Value.ToString("0.00") : hospitalProcureCatalog.Price.Value.ToString("0.0000");
                    //暂存当前配送企业，及产品产品属性
                    ViewState["CompanyID_PS"] = hospitalProcureCatalog.CompanyID_PS;
                    ViewState["SortSource"] = hospitalProcureCatalog.SortSource;
                }
                //获得医疗机构和配送企业是否有配送关系
                BoolSelect = hpcCtrl.GetBooleanHospitalPurchaseOrder(new HZ.DrugTrade.Model.Query.HospitalProcureCatalog() { ProcureCatalogID = hospitalProcureCatalog.ProcureCatalogID, Companyid_Ps = hospitalProcureCatalog.CompanyID_PS, HospitalID = hospitalProcureCatalog.HospitalID });
                //获取该采购目录所有可配送企业
                int count;
                IList<Model.View.AreaDistribution> availableList = id.GetPartDistribution(procureCatalogId, hospitalId, 1, 0, out count);

                ////原有的配送关系不用了，直接从医疗机构采购目录读取当前配送企业 by ligang 20091116
                ////获取所有绑定了的配送关系
                //int temp;
                //IList<Model.View.DistributionRelation> disrlist = BLLFactory.Factory.BLL_Distribution().GetDistributionByWhere(new HZ.DrugTrade.Model.Query.DistributionRelation() { ProcureCatalogID = procureCatalogId, HospitalID = CurrentAccount.GetUser().Org.OrgID, SortFields = "ProcureCatalogID" }, 1, 0, out temp);
                List<Model.View.DistributionRelation> bindlist = new List<HZ.DrugTrade.Model.View.DistributionRelation>();
                for (int i = 0; i < availableList.Count; i++)
                {
                    //仅绑定可选的企业
                    if (!string.IsNullOrEmpty(availableList[i].CompanyID_PS))
                    {
                        Model.View.DistributionRelation dr = new HZ.DrugTrade.Model.View.DistributionRelation();
                        dr.ProcureCatalogID = procureCatalogId;
                        dr.HospitalID = CurrentAccount.GetUser().Org.OrgID;
                        dr.CompanyID_PS = availableList[i].CompanyID_PS;
                        dr.CompanyName_PS = availableList[i].CompanyName_PS;
                        //dr.Price = hospitalProcureCatalog.ProcurePrice;//采购限价默认等于中标价，若配送关系里有其他价格则覆盖 //设置配送企业在单独页面设置  modfiy by ysm 20130130

                        if (dr.CompanyID_PS == hospitalProcureCatalog.CompanyID_PS)
                        {
                            //dr.Price = hospitalProcureCatalog.Price;
                            dr.DistributionIsUsing = true;
                        }

                        //2010-5-9 hw修改:医疗机构不设置采购限价，由盟市招标机构设置 医疗读取 如果医疗目录价格为0 则显示中标价  
                        //dr.Price = (hospitalProcureCatalog.Price == null) ? hospitalProcureCatalog.ProcurePrice : hospitalProcureCatalog.Price; //设置配送企业在单独页面设置  modfiy by ysm 20130130

                        bindlist.Add(dr);
                    }
                }

                gvwCompany.DataSource = bindlist;
                gvwCompany.DataBind();
            }
            else
            {
                IList<HZ.DrugTrade.Model.View.HospitalProcureCatalog> hospitalProcureCatalogOne = hpcCtrl.GetHospitalProcureCatalog(
                        new HZ.DrugTrade.Model.Query.HospitalProcureCatalog() { ProcureCatalogID = procureCatalogId }, 1, 0, out tmpCount);
                if (hospitalProcureCatalogOne.Count > 0)
                {
                    hospitalProcureCatalog = hpcCtrl.GetHospitalProcureCatalog(
                            new HZ.DrugTrade.Model.Query.HospitalProcureCatalog() { ProcureCatalogID = procureCatalogId }, 1, 0, out tmpCount)[0];

                    if (hospitalProcureCatalog != null)
                    {
                        this.lblGoodsName.Text = hospitalProcureCatalog.GoodsName;
                        this.lblMaterial.Text = hospitalProcureCatalog.Material;
                        this.lblGg.Text = hospitalProcureCatalog.Outlookc;
                        this.lblCompanyName_SC.Text = hospitalProcureCatalog.CompanyName_SC;
                        this.lblCompanyName_ZB.Text = hospitalProcureCatalog.CompanyName_TB;
                        this.lblBidPrice.Text = (hospitalProcureCatalog.ProcurePrice.Value == 0) ? "NA" : ViewState["Projectid"].ToString() == "0002" ? hospitalProcureCatalog.ProcurePrice.Value.ToString("0.00") : hospitalProcureCatalog.ProcurePrice.Value.ToString("0.0000");
                        this.lblPrice.Text = "NA";
                    }
                }
            }
        }

        /// <summary>
        /// 转换布尔值为checked
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected string GetChecked(object obj)
        {
            if (Convert.ToString(obj) == true.ToString())
            {
                return "checked";
            }
            return "";
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindAllCompanyList();
        }

        /// <summary>
        /// 更新配送
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwCompany_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (Page.IsValid)
            {
                int rowIndex = e.RowIndex;

                string strCompanyID_PS = ((TextBox)this.gvwCompany.Rows[rowIndex].FindControl("txtCompanyIDPs")).Text.Trim();
                string strCompanyName_PS = ((Label)this.gvwCompany.Rows[rowIndex].FindControl("txtCompanyName")).Text.Trim();

                //设置配送企业在单独页面设置  modfiy by ysm 20130130
                //string strPrice = ((TextBox)this.gvwCompany.Rows[rowIndex].FindControl("txtPrice")).Text.Trim();

                //if (!HZ.Common.MyValidate.IsMoney(strPrice))
                //{
                //    HZ.Web.WebUtil.Alert("价格格式不对！", this);
                //    ((TextBox)gvwCompany.Rows[rowIndex].FindControl("txtPrice")).Focus();
                //    return;
                //}

                //获取该条医疗机构产品目录的信息
                string procureCatalogId = Request.QueryString["procureCatalogId"];
                string hospitalId = CurrentAccount.GetUser().Org.OrgID;
                int tmpCount;
                HZ.DrugTrade.Model.View.HospitalProcureCatalog hospitalProcureCatalog = hpcCtrl.GetHospitalProcureCatalog(
                    new HZ.DrugTrade.Model.Query.HospitalProcureCatalog() { ProcureCatalogID = procureCatalogId, HospitalID = hospitalId }, 1, 0, out tmpCount)[0];

                //if (Convert.ToDecimal(strPrice) > hospitalProcureCatalog.ProcurePrice)
                //{
                //    HZ.Web.WebUtil.Alert("采购限价不能高于采购价" + hospitalProcureCatalog.ProcurePrice.Value.ToString("0.00") + "！", this);
                //    ((TextBox)gvwCompany.Rows[rowIndex].FindControl("txtPrice")).Focus();
                //    return;
                //}

                Model.HospitalProcureCatalog model = new HospitalProcureCatalog();
                model.HospitalID = CurrentAccount.GetUser().Org.OrgID;
                model.CompanyID_PS = strCompanyID_PS;
                model.CompanyName_PS = strCompanyName_PS;
                model.ProcureCatalogID = Request.QueryString["procureCatalogId"];
                //model.Price = Convert.ToDecimal(strPrice);  //设置配送企业在单独页面设置  modfiy by ysm 20130130
                if (strCompanyID_PS != hospitalProcureCatalog.CompanyID_PS)
                {
                    model.CompanyID_Last = hospitalProcureCatalog.CompanyID_PS;
                }

                int result = hpcCtrl.UpdateHospitalProcureCatalogCompanyPS(model );
                if (result == 1)
                {
                    //成功就不提示了
                    //PageInfo("配送企业设置成功！", this.ReturnUrl);
                    this.gvwCompany.EditIndex = -1;
                    HZ.DrugTrade.BLL.BLL_Log.Instance.AddKeyOperationLog(new HZ.DrugTrade.Model.KeyOperationLog() { OperateMessage = string.Format("医疗机构设置配送企业,产品编号:【{0}】,配送企业编号:【{1}】,配送企业名称：【{2}】,前配送企业：【{3}】", model.ProcureCatalogID, strCompanyID_PS, strCompanyName_PS, model.CompanyID_Last) });
                    BindAllCompanyList();
                }
                else
                {
                    PageError("设置出错！请联系管理员", this.ReturnUrl);
                }
            }
        }

        /// <summary>
        /// 编辑价格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwCompany_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.gvwCompany.EditIndex = e.NewEditIndex;
            BindAllCompanyList();
        }

        protected void gvwCompany_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.gvwCompany.EditIndex = -1;
            BindAllCompanyList();
        }

        /// <summary>
        /// 行绑定事件
        /// 1.当默认配送企业不存在时，医疗机构可以操作设置配送
        /// 2.当默认配送企业已经设置时，只可修改当前配送的价格，不可再设置其他配送企业
        /// 3.上述限制只对限价竞价产品，重点监控产品有效
        /// modify by ligang 20091122
        ///可以任意修改配送企业 注释相关语句 by wgh 20100713
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwCompany_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lbtnModify = (LinkButton)e.Row.FindControl("lbtnModify");
                if (BoolSelect)
                {
                    lbtnModify.Visible = false;
                }
            }
        }
    }
}
